// pages/wode/wode.js
const app=getApp()
//获得全局js变量
var util = require('../../utils/util.js');
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		imgurl:app.data.imgurl,
		signState:0,//签到状态
        userinfo: [],//用户信息
		userid:0,
		advnum:0,
		loading:false,
		jiange:true
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
        console.log(app.globalData);
        this.setData({
            userid: app.globalData.userId,
            userinfo: app.globalData.userinfo
        });
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
        //个人信心
        var userId = app.globalData.userId;
        if(userId == undefined || userId == 0 || userId == ''){
            
        }else{
        	this.getUserInfoForMine();
        }
	},
	initAd:function(){
		console.log(this.data.userinfo);
		if(this.data.advnum<=0){
			wx.showToast({
                title: "今日广告次数已用完",
                icon: 'none'
            })
            return;
		}
		if(!this.data.jiange){
			wx.showToast({
                title: "广告加载中，请稍后",
                icon: 'none'
            })
            return;
		}
		wx.showToast({
        title: "广告拉取中，请稍后",
        icon: 'none'
    })
		var that = this;
		let videoAd = null;
		// 在页面onLoad回调事件中创建激励视频广告实例
		if (wx.createRewardedVideoAd) {
		  	videoAd = wx.createRewardedVideoAd({
		    	adUnitId: 'adunit-0f379379892a48c2'
		  	})
		  	videoAd.onLoad(() => {
          
        })
		  	videoAd.onError((err) => {
          wx.showToast({
            title: "激励视频光告加载失败"+err.errCode+",msg:"+err.errMsg,
            icon: 'none'
          })
			    console.error('激励视频光告加载失败', err)
			})
		  	videoAd.closeHandler= function (res) {
		        if (res && res.isEnded || res === undefined) {
		        	console.log(that.data.jiange);
		        	if(that.data.jiange){
		        		that.setData({
			        		jiange:false
			        	});
			            that.addAdCoin();
		        	}
		        }
		        var num = 7;
				var	t = setInterval(play, 1000);
				function play(){
					num--;
					if(num == 0){
						clearInterval(t);
			            that.getUserInfoForMine();
						that.setData({
			        		jiange:true
			        	});
					}
				}
	      	};
	      	videoAd.onClose(videoAd.closeHandler);
		}
		// 用户触发广告后，显示激励视频广告
		if (videoAd) {
		  	videoAd.show().catch(() => {
		    // 失败重试
		    videoAd.load()
		      .then(() => videoAd.show())
		      .catch(err => {
		        console.error('激励视频 广告显示失败', err)
		      })
		  })
		}
	},
	addAdCoin:function(){
		var that=this;
        util.httpRrquest(app.data.apiurl + 'user/addWxAdvCoin', 'GET',{})
        .then(function (response) {
            wx.showToast({
                title: "观看完成",
                icon: 'none'
            })
        }, function (error) {
            console.log(error);
        });
	},
    getUserInfoForMine:function(){
        var that=this;
        util.httpRrquest(app.data.rooturl + 'weixin/getUserInfo', 'GET',{})
        .then(function (response) {
            var data = response.data.data;
            if(data.loginStatus == 2){
                app.globalData.userId = data.id;
                app.globalData.userinfo = data;
                app.globalData.userPhone = data.userPhone;
                that.setData({
                	userid:data.id,
                	userinfo:data,
                	signState:data.sign,
                	advnum:data.wadvnum
                });
            }
        }, function (error) {
            console.log(error);
        });
        
    },
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'none'
                })
            }
        }
	},
    //签到操作
	qiandao(){
        var that = this
		let state=this.data.signState
		var userid=this.data.userid;
		if(state==0){
            state=1;
            util.httpRrquest(app.data.rooturl + 'user/sign', 'GET', {userid:userid})
            .then(function (response) {
                
            }, function (error) {
                console.log(error);
            });
		}else{
			wx.showToast({
				title: '明天再来吧！',
				icon: 'none',
				duration: 2000
			})
		}
		this.setData({
			signState:state
		})
    },
    //拨打电话
	makePhoneCall(){
        var that=this
		wx.showModal({
            content: that.data.userinfo.kefu,
			confirmText:"拨打",
			success (res) {
			  if (res.confirm) {
				wx.makePhoneCall({
                    phoneNumber: that.data.userinfo.kefuphone
				})
			  } else if (res.cancel) {
				console.log('用户点击取消')
			  }
			}
		})
	}

})